﻿(function(){
var uIE7=$.browser.msie && $.browser.version < 7;

var floatPanel = window.floatPanel = function(p) {
    return new floatPanel.prototype.init(p);
}
floatPanel.prototype={
    init:function(p){
        if (!p.target) throw new Error('指定的目标不存在');
        if (typeof p.autoHide == 'undefined') p.autoHide=false;
		if (typeof p.enableEsc=='undefined') p.enableEsc=false;
		if (typeof p.autoFollow=='undefined') p.autoFollow=true;
		if (typeof p.modal=='undefined') p.modal=false;
		if (typeof p.position=='undefined') p.position='center';
		if (typeof p.width=='undefined') p.width=300;
		if (typeof p.addClose=='undefined') p.addClose=true;
		
        this.preShowed = this.isShowed=this.selfClicked=false;
		for(var k in p)this[k]=p[k];
		var self = this;
		this.target.bind("click", function(){self.selfClicked=true;});
		this.target.width(p.width);
		if (p.autoFollow && !uIE7) this.target.css('position','fixed');
		if (p.addClose) {
		    var a = $(document.createElement('a'));
		    a.addClass('close');
		    a.html('&nbsp;');
		    a.click(function(){self.hide();});
		    a.appendTo(this.target);
		}
        return this;
    },
    show:function(sender, evt){
        if (this.modal) maskPanel.show(this.target);
        else {
            this._zIndex = this.target.css('z-index');
		    this.target.css('z-index', 50001);
        }
        evt = evt || window.event;
		this.caller = sender;
		this.target.show();
        this.isShowed = false;
		this.preShowed = true;
		
		this.setPosition();
        if(typeof this.onShow == 'function')this.onShow.call(this, sender, evt);
		
		var self=this;
       	if (this.autoHide) {
			if (!this.__click) this.__click=function(){self._click.call(self, evt);};
			$(document).bind("click", this.__click);
		}
      	if (this.enableEsc) {
			if (!this.__keyup) this.__keyup = function(evt){evt=evt||window.event;self._keyup.call(self,evt)};
			$(document).bind("keyup", this.__keyup);
		}
		
		if (uIE7 && this.autoFollow) {
			if (!this.__follow) this.__follow = function(evt){evt=evt||window.event;self._follow.call(self, evt);};
			$(window).bind("scroll", this.__follow);
		}
		this.__resize=function(evt){evt=evt||window.event;self._resize.call(self, evt);};
		$(window).bind('resize', this.__resize);
		this._top = $(document).scrollTop();
    },
    _resize:function(evt){
        maskPanel.resize();
        this.setPosition();
    },
    _click:function(evt){
		if (this.isShowed && !this.selfClicked) {
			this.hide();return false;
		}
		if (this.preShowed) this.isShowed=true;
		this.preShowed = false;
		this.selfClicked = false;
    },
	_keyup:function(evt){
		if (evt.keyCode==27)this.hide();
	},
	_follow:function(evt){
		this.target.css('top',(parseInt(this.target.css('top'),10) + $(document).scrollTop() - this._top));
		this._top = $(document).scrollTop();
	},
    hide:function(evt){
        if (this.modal) maskPanel.hide();
        else this.target.css('z-index', this._zIndex);
		evt = evt || window.event;
        this.target.hide();
        this.isShowed=false;
		this.autoHide && $(document).unbind("click", this.__click);
		this.enableEsc && $(document).unbind("keyup", this.__keyup);
		uIE7 && this.autoFollow && $(window).unbind('scroll', this.__follow);
		$(window).unbind('resize', this.__resize);
		typeof this.onHide == 'function' && this.onHide.call(this, this.caller, evt);
    },
    setPosition:function(pos){
        pos = pos || this.position;
        var wnd = $(window), doc = $(document), top = doc.scrollTop(), left = doc.scrollLeft();

		if (pos.constructor == Array) {
			top += pos[1];
			left += pos[0];
		} else {
		    var _left,_top;
			switch (pos) {
				case 'center':
					_top = (wnd.height() / 2) - (this.target.height() / 2);
					_left = (wnd.width() / 2) - (this.target.width() / 2);
					break;
				case 'top':
					_top = 0;
					left += (wnd.width() / 2) - (this.target.width() / 2);
					break;
				case 'right':
					_top = (wnd.height() / 2) - (this.target.height() / 2);
					_left = (wnd.width()) - (this.target.width());
					break;
				case 'bottom':
					_top = (wnd.height()) - (this.target.height());
					_left = (wnd.width() / 2) - (this.target.width() / 2);
					break;
				case 'left':
					_top = (wnd.height() / 2) - (this.target.height() / 2);
					_left = 0;
					break;
				default:
					//center
					_top = (wnd.height() / 2) - (this.target.height() / 2);
					_left = (wnd.width() / 2) - (this.target.width() / 2);
			}
		    if (this.autoFollow && !uIE7) {
		        top = _top;
		        left = _left;
		    } else {
		        top += _top;
		        left += _left;
		        top = top < doc.scrollTop() ? doc.scrollTop() : top;
		    }
		}

		this.target.css({top: top, left: left});
    }
};
floatPanel.prototype.init.prototype=floatPanel.prototype;

window.maskPanel = {
	_id:'__window_mask_',
	_inited:false,
	show: function(target){
		var div;
		if (!(div = $('#' + this._id)[0])) {
			div = document.createElement('div');
			div.id = this._id;
			div.style.cssText = 'position:absolute;z-index:50000;left:0;top:0;width:100%;background:#000;opacity:0.35;filter:Alpha(Opacity:35);display:none;';
			document.body.appendChild(div);
		}
		if (!this._inited) {
			this.resize.call(div);
			$(window).bind('resize', this.resize);
			this._inited = true;
		}
		$(div).show();
		uIE7 && (function(){
		    var s = document.getElementsByTagName('select');
		    $.each(s, function(){
		        $(this).css('visibility','hidden');
		    });
		})();
		
		if (target) {
		    this._zIndex = target.css('z-index');
		    target.css('z-index', 50001);
		}
		this._target = target;
	},
	hide:function(){
		$('#' + this._id).hide();
		$(this._target).css('z-index',this._zIndex);
		$(window).unbind('resize', this.resize);
		uIE7 && (function(){
		    var s = document.getElementsByTagName('select');
		    $.each(s, function(){
		        $(this).css('visibility','visible');
		    });
		})();
	},
	resize:function(){
		var mask = $('#' + maskPanel._id),wnd=$(document);
		mask.width(0);
		mask.height(0);
		if ($.browser.msie && $.browser.version < 7) {
		    mask.width(Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth));
		    mask.height(Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight));
		    return;
		};
		mask.width(wnd.width());
		mask.height(wnd.height());
	}
};

})()